package com.geekchique.runningbuddy.activity.stopclock;

import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextSwitcher;
import android.widget.TextView;

import com.geekchique.runningbuddy.R;
import com.geekchique.runningbuddy.R.id;
import com.geekchique.runningbuddy.R.layout;
import com.geekchique.runningbuddy.model.JogEntry;
import com.geekchique.runningbuddy.provider.ActivityID;
import com.geekchique.runningbuddy.service.TimeRunnerService;
import com.geekchique.runningbuddy.util.DBHelper;
import com.geekchique.runningbuddy.view.StopClockTargetView;
import com.geekchique.runningbuddy.view.StopClockView;

public class StopClock extends Activity {
    
    protected StopClockThread timerRunnerThread;

	private StopClockTargetView stopClockTargetView;
	private StopClockView stopClockView;
	private DBHelper dbHelper;
	private long startTime;
	private long stopTime;
	
	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
        dbHelper = new DBHelper(this);
        
        setContentView(R.layout.stopclock);
		createStopClockView();
		createStopClockTargetView();
	}
	
	
    private void createStopClockView() {
		
		stopClockView = new StopClockView(
								this,
								(TextSwitcher) findViewById(R.id.stopclock_time),
								(Button) findViewById(R.id.stopclock_buttonstart)   		
							);
		
	    stopClockView.setStartStopButtonClickListener(executeAStart);
        stopClockView.resetStopClock();
	}
    
	private void createStopClockTargetView() {

        timerRunnerThread = new StopClockThread(stopClockServiceHandler);
    	
		stopClockTargetView = new StopClockTargetView(
				this,
				(CheckBox) findViewById(R.id.checkbox_target),
				(TextView) findViewById(R.id.hours),
				(TextView) findViewById(R.id.minutes),
				(TextView) findViewById(R.id.seconds),
				(TextView) findViewById(R.id.milliseconds),
				(Button) findViewById(R.id.hours_up),
				(Button) findViewById(R.id.hours_down),
				(TextView) findViewById(R.id.hours_sep),
				(Button) findViewById(R.id.minutes_up),
				(Button) findViewById(R.id.minutes_down),
				(TextView) findViewById(R.id.minutes_sep),
				(Button) findViewById(R.id.secs_up),
				(Button) findViewById(R.id.secs_down),
				(TextView) findViewById(R.id.seconds_sep),
				(Button) findViewById(R.id.millisecs_up),
				(Button) findViewById(R.id.millisecs_down),
				timerRunnerThread
		);

		timerRunnerThread.turnOffASetTargetTime();  	
		stopClockTargetView.hideSetTargetTime();
    	
	}
    
	private void updateRunningStopClock() {
		String elapsedTime = timerRunnerThread.getElapsedTime();
		
		if (timerRunnerThread.targetIsSet() && isTargetTimeReached(elapsedTime)) {
			stopStopClock();
			stopClockView.setStopClock(timerRunnerThread.getTargetTime());
		} else {
			stopClockView.setStopClock(elapsedTime);
		}
	}


	private void startStopClock() {
		timerRunnerThread.start();
		startTime = System.currentTimeMillis();
		startService(new Intent(StopClock.this, TimeRunnerService.class), null);
		stopClockView.switchStopClockButton(ActivityID.STOPCLOCK_RUNNING);
		stopClockView.setStartStopButtonClickListener(executeAStop);
	}


	private void stopStopClock() {
		timerRunnerThread.stop();
		stopTime = System.currentTimeMillis();
		stopService(new Intent(StopClock.this, TimeRunnerService.class));
		createNewJogLogEntry("jog" + new Date().getTime(), startTime, stopTime, 999999, 999999, 99, 99);
		stopClockView.switchStopClockButton(ActivityID.STOPCLOCK_NOT_RUNNING);
		stopClockView.setStartStopButtonClickListener(executeAStart);
	}


	private boolean isTargetTimeReached(String elapsedTime) {
		int stopClock = StopClockTools.compare(timerRunnerThread.getTargetTime(), elapsedTime);
		
		return stopClock == StopClockTools.EQUAL	|| stopClock == StopClockTools.GREATER_THAN;
	}


	protected void createNewJogLogEntry(String name, long startTime, long stopTime, long longitude, long latitude, int cellTowerId, int locationAreaCode) {
		JogEntry jogEntry = new JogEntry(name, startTime, stopTime, latitude, longitude, cellTowerId, locationAreaCode);
		dbHelper.addJogEntries(jogEntry);
	}

	protected Handler stopClockServiceHandler = new Handler() {

		public void handleMessage(Message msg) {
			switch (msg.what) {
			case ActivityID.UPDATESTOPCLOCK:
				updateRunningStopClock();
				break;
			}
			super.handleMessage(msg);
		}

	};
	
	private OnClickListener executeAStop = new OnClickListener() {
		public void onClick(View v) {
			stopStopClock();
		}
	};
	
	private OnClickListener executeAStart = new OnClickListener() {
		public void onClick(View v) {
			startStopClock();
		}

	};

}
